package algorithms.que13;

import edu.princeton.cs.algs4.StdOut;

/**
 * @功能
 * @创建时间 2020/8/1
 */

public class MoveToFront1<Item> {

    /**
     * @param args
     */
    private Node first;
    private int N;

    private class Node {
        Item item;
        Node next;
    }

    public boolean isEmpty() {
        return first == null;
    }

    public void add(Item item) {
        Node node = new Node();
        node.item = item;

        if (first != null && first.item == item) return;

        Node cur = first;
        for (; cur != null && cur.next != null; cur = cur.next) {
            if (cur.next.item == item) {
                Node temp = cur.next;
                cur.next = cur.next.next;
                temp.next = null;
                break;
            }
        }
        node.next = first;
        first = node;

    }

    public Item delete() {
        Node temp = first;
        Item item = first.item;
        first = first.next;
        temp.next = null;
        return item;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MoveToFront1<Integer> m = new MoveToFront1<Integer>();
        for (int i = 0; i < 10; i++)
            m.add(i);
        m.add(5);
        m.add(8);
        while (!m.isEmpty())
            StdOut.println(m.delete());
    }

}